package com.jrx.anytxn.param.controller.investor;

import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.param.dto.investor.PrInvestorPreterminationFeeReq;
import com.jrx.anytxn.param.dto.investor.PrInvestorPreterminationFeeRes;
import com.jrx.anytxn.param.service.investor.IPrInvestorPreterminationFeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;

/**
 * 出资方提前结清手续费表 控制器
 *
 * @author 黄秋平
 * @date 2020-05-20 11:14
 **/
@Api(tags = "页面出资方提前结清费参数API接口")
@RestController
@RequestMapping(value = "/param/investor/preterminationFees")
public class PrInvestorPreterminationFeeController {

    private static final Logger logger = LoggerFactory.getLogger(PrInvestorPreterminationFeeController.class);
    
    @Resource
    IPrInvestorPreterminationFeeService iPrInvestorPreterminationFeeService;
    
    @Resource
    private InfoService infoService;

    @ApiOperation(value = "根据租户ID和费用表ID查询出资方提前结清费表信息", notes = "根据租户ID和费用表ID查询出资方提前结清费表信息")
    @GetMapping(value = "/{tenantId}/{investorPreterminationFeeId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "investorPreterminationFeeId", value = "出资方费用表ID", required = true)
    })
    public TxnRespResult<PrInvestorPreterminationFeeRes> getByTenIdAndFeeId(@NotNull @PathVariable String tenantId, @PathVariable String investorPreterminationFeeId) {
        TxnRespResult<PrInvestorPreterminationFeeRes> result = new TxnRespResult<>();
        PrInvestorPreterminationFeeRes res;
        try {
            res = iPrInvestorPreterminationFeeService.findByTenIdAndFeeId(String.valueOf(infoService.getUser().getProjectId()), investorPreterminationFeeId);
                if (res.getFeeChargePercent() != null && res.getFeeChargePercent().compareTo(BigDecimal.valueOf(0)) != 0) {
                    res.setFeeChargePercent(res.getFeeChargePercent().multiply(BigDecimal.valueOf(100)));
                }
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID分页查询出资方提前结清费表信息", notes = "根据租户ID分页查询出资方提前结清费表信息")
    @GetMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true),})
    public TxnRespResult<TxnPage<PrInvestorPreterminationFeeRes>> getAllPageByTenId(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize) {

        TxnRespResult<TxnPage<PrInvestorPreterminationFeeRes>> result = new TxnRespResult<>();
        TxnPage<PrInvestorPreterminationFeeRes> response;
        try {
            String tenantId= String.valueOf(infoService.getUser().getProjectId());
            response = iPrInvestorPreterminationFeeService.findAllPageByTenId(pageNum, pageSize, tenantId);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "新增出资方提前结清费表信息", notes = "新增出资方提前结清费表信息")
    @PostMapping("/")
    public TxnRespResult<PrInvestorPreterminationFeeRes> createInvestorPreterminationFee(@RequestBody PrInvestorPreterminationFeeReq prPreterminationFeeTableReq) {
        PrInvestorPreterminationFeeRes res;
        TxnRespResult<PrInvestorPreterminationFeeRes> result = new TxnRespResult<>();
        try {
            prPreterminationFeeTableReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            prPreterminationFeeTableReq.setUpdateBy(infoService.getUser().getUserName());
            if (prPreterminationFeeTableReq.getFeeChargePercent() != null && prPreterminationFeeTableReq.getFeeChargePercent().compareTo(BigDecimal.ZERO) != 0) {
                prPreterminationFeeTableReq.setFeeChargePercent(prPreterminationFeeTableReq.getFeeChargePercent().divide(BigDecimal.valueOf(100)));
            }
            res = iPrInvestorPreterminationFeeService.addPreterminationFeeTable(prPreterminationFeeTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "编辑出资方提前结清费表信息", notes = "编辑出资方提前结清费表信息")
    @PutMapping("/")
    public TxnRespResult<PrInvestorPreterminationFeeRes> modifyInvestorPreterminationFee(@RequestBody PrInvestorPreterminationFeeReq prPreterminationFeeTableReq) {
        TxnRespResult<PrInvestorPreterminationFeeRes> result = new TxnRespResult<>();
        PrInvestorPreterminationFeeRes res;
        try {
            prPreterminationFeeTableReq.setUpdateBy(infoService.getUser().getUserName());
            if (prPreterminationFeeTableReq.getFeeChargePercent() != null && prPreterminationFeeTableReq.getFeeChargePercent().compareTo(BigDecimal.ZERO) != 0) {
                prPreterminationFeeTableReq.setFeeChargePercent(prPreterminationFeeTableReq.getFeeChargePercent().divide(BigDecimal.valueOf(100)));
            }
            res = iPrInvestorPreterminationFeeService.modifyPreterminationFeeTable(prPreterminationFeeTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "删除出资方提前结清费表信息", notes = "根据id删除出资方提前结清费表信息")
    @DeleteMapping(value = "/{tenantId}/{investorPreterminationFeeId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "investorPreterminationFeeId", value = "出资方费用表ID", required = true)
    })
    public TxnRespResult<Boolean> removeByTenIdAndFeeId(@NotNull @PathVariable String tenantId, @PathVariable String investorPreterminationFeeId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            flag = iPrInvestorPreterminationFeeService.removeByTenIdAndFeeId(String.valueOf(infoService.getUser().getProjectId()), investorPreterminationFeeId);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }



}
